<template>
	<view>
			<web-view  :src="data.url"></web-view>
		
	</view>
</template>

<script setup>
	import {
		ref,
		getCurrentInstance,
		reactive
	} from "vue";
	import {
		onLoad,
		onShow,
		onUnload
	} from "@dcloudio/uni-app";
	const id = ref('') //onload传入ID
	const money = ref('') //金额
	const key = ref(true) //数字键盘
	const notes = ref('') //备注
	const orderID = ref(true) //订单编号
	const butdsb = ref(false) //支付按钮禁用
	const code = ref('') //获取code
	const type = ref(2) //0微信 1支付宝 2微信公众号
	const newCode = ref(1) //0微信 1公众号
	const is_h5 = ref(3) //0 app 1 h5 2 微信小程序 3 微信公众号
	const open_id = ref('')
	let data = reactive({
		url:"",
		merchant:'',
		order:'',
		money:'',
		logo: '',
		name: '',
		title: '',
	})
	onLoad((opt) => {
		// data.url = `https://tool.guanxikeji.com/scan?merchant=${opt.merchant}&order=${opt.order}&money=${opt.money}`
		data.merchant = opt.merchant
		data.order = opt.order
		data.money = opt.money
	})
	onShow(() => {
		code.value = getQueryString("code"); //获取code
		getData()
	})
	function getData(){
		appContext.config.globalProperties.$request({
			// url: '/admin/wxPay/?id=' + id.value,
			url: `/admin/wxPay/?id=${data.merchant}&code=${code.value}&types=2`,
			method: 'GET',
			success: (res) => {
				if (res.errno == 0) {
					console.log("Pay商家信息：", res.data)
					data.logo = res.data.logo
					data.name = res.data.name
					data.title = res.data.title
					id.value = res.data.ID
					open_id.value = res.data.open_id
						pay()
				} else {
					console.log("获取商家信息失败：", res)
					if (appContext.config.globalProperties.$isWeiXinBrowser()) {
						wxLogin() //调用获取code
					}
				}
			},
			fail(err) {
				console.error('获取支付信息失败', err);
				uni.showToast({
					icon: 'none',
					title: '获取支付信息失败',
					duration: 1500,
				})
			}
		})
	}
	// 获取url参数code方法
	function getQueryString(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
		var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
		var context = "";
		if (r != null) context = r[2];
		reg = null;
		r = null;
		return context == null || context == "" || context == "undefined" ?
			"" :
			context;
	}
	// 调起微信支付
	function pay() {
	
	
	
		// 公众号
		if (appContext.config.globalProperties.$isWeiXinBrowser()) {
			open_id.value = uni.getStorageSync("open_id")
			if (!open_id.value) {
				uni.showToast({
					icon: 'none',
					title: 'open_id过期,刷新中',
					duration: 1500,
				})
				wxLogin()
				return
			}
			if (!code.value) {
				uni.showToast({
					icon: 'none',
					title: 'code过期,刷新中',
					duration: 1500,
				})
				wxLogin() //调用获取code
				return
			}
		}
	
	

	
		appContext.config.globalProperties.$request({
			url: '/admin/wxPay/',
			method: 'POST',
			data: {
				open_id: open_id.value,
				id: parseFloat(id.value),
				money: parseFloat(data.money),
				notes: notes.value,
				order: order,
				project: project,
				is_h5: is_h5.value,
				code: newCode.value,
			},
			success: (res) => {
				console.log('提交信息回调成功', res);
				var data = res.data
				if (res.errno == 0) {
					if (appContext.config.globalProperties.$isWeiXinBrowser()) {
						// 公众号↓↓↓
						try {
							console.log('WeixinJSBridge', WeixinJSBridge)
							WeixinJSBridge.invoke('getBrandWCPayRequest', {
								"appId": data.appid, //公众号名称，由商户传入     
								"timeStamp": data.timeStamp, //时间戳，自1970年以来的秒数     
								"nonceStr": data.nonceStr, //随机串     
								"package": data.package,
								"signType": data.signType, //微信签名方式：     
								"paySign": data.paySign //微信签名 
							}, function(res) {
								if (res.err_msg == "get_brand_wcpay_request:ok") {
									//支付成功
									uni.showToast({
										icon: 'none',
										title: '支付成功',
										duration: 1500,
									})
								
								} else if (res.err_msg == "get_brand_wcpay_request:cancel") {
									//支付取消
									uni.showToast({
										icon: 'none',
										title: '支付取消',
										duration: 1500,
									})
									postFail()
									butdsb.value = false //启用支付按钮
								} else if (res.err_msg == "get_brand_wcpay_request:fail") {
									//支付失败
									uni.showToast({
										icon: 'none',
										title: '支付失败',
										duration: 1500,
									})
									postFail()
									butdsb.value = false //启用支付按钮
								} //res.err_msg将在用户支付成功后返回ok
							});
						} catch (e) {
							uni.showToast({
								icon: 'none',
								title: '支付失败!',
								duration: 1500,
							})
							postFail()
							console.log("错误捕获", e)
							butdsb.value = false //启用支付按钮
						}
					} 
				}
				if (res.errno == 1) {
					uni.showToast({
						icon: 'none',
						title: res.errmsg,
						duration: 1500,
					})
					butdsb.value = false //启用支付按钮
				}
				if (res.errno == 2) {
					uni.showToast({
						icon: 'none',
						title: 'code有误,重新获取中',
						duration: 1500,
					})
					getinfo()
					butdsb.value = false //启用支付按钮
					return
				}
			},
			fail: (err) => {
				console.log('failed', err);
				uni.showToast({
					icon: 'none',
					title: '支付失败',
					duration: 1500,
				})
				butdsb.value = false //启用支付按钮
			}
		});
		console.log("notes备注：", notes.value)
	}
</script>

<style>
</style>